package com.hanbang.maige.code;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ImageView;

import com.baidu.mapapi.SDKInitializer;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hanbang.maige.BuildConfig;
import com.hanbang.maige.R;
import com.hanbang.maige.code.broadcast.NetWorkBroadcastReceiver;
import com.hanbang.maige.utils.bitmap.GlideUtils;
import com.hanbang.maige.utils.db.LiteOrmUtil;
import com.hanbang.maige.utils.http.httpquest.HttpLocalUtils;
import com.hanbang.maige.widget.empty_layout.LoadingAndRetryManager;
import com.hubang.ninegrid.NineGridView;

import java.io.File;

import butterknife.ButterKnife;
import me.drakeet.library.CrashWoodpecker;

public class MyApplication extends Application {
    public static Application myApp;
    public NetWorkType netWorkType = NetWorkType.NULL;
    public static String appCachePath;
    public static String appFilePath;
    public static String appSDCachePath;
    public static String appSDFilePath;

    public enum NetWorkType {
        WIFI, ETHERNET, MOBILE, NetWorkType, NULL
    }


    @Override
    public void onCreate() {
        super.onCreate();
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        //注意该方法要再setContentView方法之前实现
        SDKInitializer.initialize(getApplicationContext());
        LiteOrmUtil.init(this);
        ButterKnife.setDebug(BuildConfig.DEBUG);
        myApp = this;
        GlideUtils.init(myApp);
        CrashWoodpecker.init(this);
        new NetWorkBroadcastReceiver(this);

        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        //typeface = Typeface.createFromAsset(getAssets(), "fzlt.ttf");
        appCachePath = getCacheDir().getPath();
        appFilePath = getFilesDir().getPath();
        appSDCachePath = Environment.getExternalStorageDirectory().getPath() + "/" + getResources().getString(R.string.app_name_letter) + "/cache";
        File file = new File(appSDCachePath);
        appSDFilePath = Environment.getExternalStorageDirectory().getPath() + "/" + getResources().getString(R.string.app_name_letter) + "/file";
        File file2 = new File(appSDFilePath);
        if (file2.exists() || file2.mkdirs())
            ;
        File fff = new File(appSDFilePath);
        if (fff.exists() || fff.mkdirs())
            ;

        LoadingAndRetryManager.BASE_EMPTY_LAYOUT_ID = R.layout.base_load_empty;
        LoadingAndRetryManager.BASE_RETRY_LAYOUT_ID = R.layout.base_load_retry;
        LoadingAndRetryManager.BASE_LOADING_LAYOUT_ID = R.layout.base_load_loading;


        registerActivityLifecycleCallbacks(activityLifecycleCallbacks);

        NineGridView.setImageLoader(new NineGridView.ImageLoader<String>() {
            @Override
            public void onDisplayImage(Context context, ImageView imageView, String o, boolean isSingle) {
                if (isSingle) {
                    Glide.with(context).load(HttpLocalUtils.getHttpFileUrl(o))
                            .fitCenter()
                            .sizeMultiplier(1f)
                            .placeholder(R.mipmap.default_1_1)
                            .error(R.mipmap.default_1_1)
                            .diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
                } else {
                    Glide.with(context).load(HttpLocalUtils.getHttpFileUrl(o))
                            .centerCrop()
                            .sizeMultiplier(1f)
                            .crossFade()
                            .placeholder(R.mipmap.default_1_1)
                            .error(R.mipmap.default_1_1)
                            .diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
                }
            }
        });

    }


    public void onTerminate() {
        super.onTerminate();
    }


    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            ActivityManager.getInstance().pushActivity(activity);
        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            ActivityManager.getInstance().exitActivity(activity);
        }
    };


}
